//模块化 store

//引入获取商品的请求方法
import * as api from "&/api"; //&符号指向src
export default {
  namespaced: true, //开启命名空间
  state: {
    productData: [],
  },
  mutations: {
    //用于更改productData的mutation
    SET_PRODUCT_DATA(state, payload) {
      state.productData = payload;
    },
    //让库存减少
    DECREASE_INVENTORY(state, payload) {
      //payload接收的是库存减少的这条数据的id
      console.log(payload);
      //通过payload到productData中查找对应的数据，让inventory减一
      let target = state.productData.find((item) => item.id === payload);
      if (target) {
        target.inventory--;
      }
    },
  },
  actions: {
    //使用action获取异步数据
    async FETCH_PRODUCT(context) {
      let res = await api.fetchProduct();
      console.log(res);
      context.commit("SET_PRODUCT_DATA", res.data);
    },
  },
  getters: {},
};
